package com.project.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.project.entity.DeptInfo;
import com.project.service.DeptInfoService;

/**
 * 
 * @author beryll@synnex.com
 * 
 */
@Controller
@RequestMapping(value = "/deptinfo")
public class DeptInfoController {
	@Resource(name = "deptInfoService")
	private DeptInfoService service;

	@RequestMapping(value = "/add")
	public String add() {
		return "deptinfo/edit";
	}

	@RequestMapping(value = "/update")
	public String update(String id, Model model) {
		DeptInfo deptInfo = service.get(Integer.parseInt(id));
		model.addAttribute("deptInfo", deptInfo);
		return "deptinfo/edit";
	}

	@RequestMapping(value = "/save")
	public String save(DeptInfo deptInfo) {
		if (StringUtils.isBlank(deptInfo.getName())) {
			return "error";
		}
		if (deptInfo.getId() == null) {
			service.saveOrUpdate(deptInfo);
		} else {
			service.get((deptInfo.getId()));
			service.saveOrUpdate(deptInfo);
		}
		return "redirect:/deptinfo/list";
	}

	@RequestMapping(value = "/list")
	public String list(Model model) {
		List<DeptInfo> list = service.listAll();
		model.addAttribute("list", list);
		return "deptinfo/list";
	}

}
